const React = require('react');
import{Router,Route,hashHistory,IndexRoute} from 'react-router';
var $=require('jquery');

var BookToTop=require('./bookToTop.jsx');
var BookNav=require('./bookNav.jsx');
var css=require('../book.css');

var App = React.createClass({
	getInitialState: function() {
		return {
		  themeNight:false
		};
	},
	getDefaultProps: function() {
    	return {
	      uid: 0
	    };
  	},
  	componentDidMount:function(){	

        //网站theme保存到localStore里
        let theme=window.localStorage.getItem("theme")||false;
        if(theme)
        {
        	if(theme==="true")
        	{
        		theme=true;
        		this.setState({
		        	themeNight:theme
		        })
		        var doc=document.querySelector("html");
		        this.addClass(doc,"night");
        	}
        }  

         
	},
  	changeTheme:function(){
  		var doc=document.querySelector("html");
  		this.toggleClass(doc,'night');
  		let newTheme=!this.state.themeNight;
        this.setState({
        	themeNight:newTheme
        });
         console.log("changeTheme="+newTheme);
         window.localStorage.setItem("theme",newTheme);

  	},
  	hasClass:function(obj, cls) {  
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));  
	},
	addClass:function(obj, cls) {  
	    if (!this.hasClass(obj, cls)) obj.className += " " + cls;  
	}, 
	removeClass:function(obj, cls) {  
	    if (this.hasClass(obj, cls)) {  
	        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');  
	        obj.className = obj.className.replace(reg, ' ');  
	    }  
	}, 
	toggleClass:function(obj,cls){  
	    if(this.hasClass(obj,cls)){  
	        this.removeClass(obj, cls);  
	    }else{  
	        this.addClass(obj, cls);  
	    }  
	},
  	render: function () {
	    return (
	       <div className="pageBox">
                <BookNav changeTheme={this.changeTheme} />
                <div className="ContentBox">
                    {this.props.children}
                </div>
                <BookToTop />
            </div>	     
	    );
	  }
});

module.exports = App;